package udp_client;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;


import com.shaolei.basic.BASIC;

public class myDisplay implements Runnable{
	public void run(){
		ImageFrame frame = new ImageFrame();
		while(true){
			frame.updateFrame();
			frame.repaint();
		}
	}
}

class ImageFrame extends JFrame
{
	public static final int DEFAULT_WIDTH = 380;
	public static final int DEFAULT_HEIGHT = 320;
	private ImageComponent component = new ImageComponent();
	public ImageFrame()
   {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setTitle("ImageTest");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		add(component);
   }
	public void updateFrame(){
		component.UpdateComponent();
	}
}
class ImageComponent extends JComponent
{
	private static Image image;
	private byte[] input = new byte[BASIC.get_IMAGESIZE()];
	public void UpdateComponent()
	{
      try
      {
    	  if(myGather.get_myCount() > BASIC.get_PKGS()*0.9) {
    		  input=myGather.get_myContainer();
    		  myGather.set_myCount(0);
    	  }
    	  if(myGather.get_myCount2() > BASIC.get_PKGS()*0.9) {
    		  input=myGather.get_myContainer2();
    		  myGather.set_myCount2(0);
    	  }
    	  ByteArrayInputStream in = new ByteArrayInputStream(input); 
    	  image = ImageIO.read(in);
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
}
   public void paintComponent(Graphics g)
   {
      if (image == null) return;
      g.drawImage(image, 0, 0, null);
   }
}